<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item ><span style="color: #ccc">订单列表</span></el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <el-row :gutter="30" style="margin-bottom:20px">
        <!-- 搜索框 -->
        <el-col :span="8">
          <el-input
            v-model="queryInfo.query"
            clearable
            placeholder="请输入内容"
            @clear="getOrdersList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrdersList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 渲染商品数据列表区 -->
      <el-table :data="ordersList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" width="60"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="auto">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格(￥)" width="110">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否支付" width="110">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="90">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="170">
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-tooltip
              effect="dark"
              content="修改订单信息"
              placement="top"
              :enterable="false"
              :open-delay="400"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                size="mini"
                @click="showEditDialog(scope.row)"
              ></el-button>
            </el-tooltip>

            <!-- 查看物流信息按钮 -->
            <el-tooltip
              effect="dark"
              content="查看物流信息"
              placement="top"
              :enterable="false"
              :open-delay="400"
            >
              <el-button
                type="danger"
                icon="el-icon-location"
                circle
                size="mini"
                @click="showProgress"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      title="修改订单信息"
      :visible.sync="editDialogVisiable"
      width="50%"
      @close="editFormClose"
    >
      <!-- 主体内容区 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="auto"
      >
        <el-form-item label="省市区/县 :" prop="address1">
          <el-cascader
            style="width:100%"
            expandTrigger="hover"
            v-model="editForm.address1"
            :options="cityData"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址 :" prop="address2">
          <el-input v-model="editForm.address2"> </el-input>
        </el-form-item>
        <el-form-item label="订单价格(￥) :" prop="order_price">
          <el-input v-model="editForm.order_price" type="number"> </el-input>
        </el-form-item>
        <el-form-item label="是否支付 :" prop="pay_status">
          <el-input v-model="editForm.pay_status" type="number"> </el-input>
        </el-form-item>

        <el-form-item label="是否发货 :" prop="is_send">
          <el-input v-model="editForm.is_send"> </el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisiable = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息 对话框 -->
    <el-dialog
      title="物流信息"
      :visible.sync="progressDialogVisiable"
      width="50%"
    >
      <el-empty description="物流模块正在开发中..."></el-empty>
    </el-dialog>
  </div>
</template>

<script>
import cityData from "./citydata.js"; //引入省市区/县的js文件

export default {
  name: "Order",
  data() {
    return {
      // 获取订单列表的参数对象
      queryInfo: {
        query: "",
        pagenum: 1, //当前页码
        pagesize: 10, // 每页展示多少条数据
      },
      // 保存获取到的订单列表数据
      ordersList: [],
      total: 0, //用来保存总的数据条数
      editDialogVisiable: false,
      consignee_addr: {
        address1: [],
      },
      editForm: {
        address1: [],
        address2: "",
      },
      // 编辑表单验证规则
      editFormRules: {
        address1: [
          { required: true, message: "请选择省市区/县", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        is_send: [
          {
            required: true,
            default: "否",
            message: "是否发货",
            trigger: "blur",
          },
        ],
        pay_status: [
          { required: true, message: "0:未付款 1:已付款", trigger: "blur" },
        ],
      },

      cityData, // 将引入的地区js文件存放到data中
      progressDialogVisiable: false, //控制物流信息对话框的显示与隐藏
    };
  },

  created() {
    this.getOrdersList();
  },

  methods: {
    // 获取订单数据列表
    async getOrdersList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取订单数据失败");
      }

      // 先调用时间戳函数，将时间戳转换格式
      res.data.goods.forEach((item) => {
        item.create_time = this.dateFormat(item.create_time);
      });
      console.log(res.data);

      // 将数据保存到data中的 ordersList 数组中
      this.ordersList = res.data.goods;
      this.total = res.data.total;
    },

    // 点击修改按钮，显示修改对话框。查看订单详情
    async showEditDialog(row) {
      const { data: res } = await this.$http.get("orders/" + row.order_id);

      if (res.meta.status !== 200) {
        return this.$message.error("获取订单列表数据失败");
      }

      this.editForm = res.data;
      console.log(this.editForm);
      this.editDialogVisiable = true;
    },

    //点击编辑对话框中的确认按钮，保存修改内容
    saveEdit() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        console.log(this.editForm);
        const { data: res } = await this.$http.put(
          "orders/" + this.editForm.order_id,
          this.editForm
        );
        // console.log(res);
        if (res.meta.status !== 201) {
          return this.$message.error("更新失败");
        }
        this.$message.success("更新成功");
        this.editDialogVisiable = false;
        this.getOrdersList();
      });
    },

    // 点击查看物流信息按钮，显示物流信息对话框
    showProgress() {
      this.progressDialogVisiable = true;
    },

    // 监听表单关闭事件
    editFormClose() {
      this.$refs.editFormRef.resetFields();
    },

    // 监听 pagesize 改变事件 （监听当前每页显示多少条数据）
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrdersList();
    },

    // 监听 页码值 改变的事件    （当前所在的页数）
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrdersList();
    },

    // 将时间戳变成想要的日期格式
    dateFormat(row) {
      var date = new Date(row); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D = date.getDate() + " ";
      var h = date.getHours() + ":";
      var m = date.getMinutes() + ":";
      var s = date.getSeconds();
      return Y + M + D + h + m + s;
    },
  },
};
</script>

<style></style>
